//
//  MusicAllAndAloneViewController.m
//  CCBbelefitLife
//
//  Created by 小晓 on 16/1/10.
//  Copyright © 2016年 liurg. All rights reserved.
//

#import "MusicAllAndAloneViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "MusicChorusTableViewCell.h"
#import "KSingingRecordViewCtr.h"
#import "MusicPalyViewController.h"
#import "KMusicMsgDataModel.h"
#import "SoloMusicView.h"
#import "YYplayerVC.h"

@interface MusicAllAndAloneViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UIView *choruslineView;
    UIView *sololineView;
}

@property ( strong , nonatomic ) AFHTTPRequestOperationManager * httpManager;
@property(strong,nonatomic)SoloMusicView * soloView;
@property(strong,nonatomic)UITableView *chorusTableView;
@property(strong,nonatomic)NSMutableArray *dataArray;
@property(strong,nonatomic)NSMutableArray *aloneArray;

@end

@implementation MusicAllAndAloneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupView];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationItem.title = _titleName;
    [self.navigationController.navigationBar setHidden:NO];
    //[AppDelegate ShowTabarView:NO];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(backBtnAction)];
    self.tabBarController.tabBar.hidden = NO;
}

-(void)backBtnAction{
    [self.navigationController popViewControllerAnimated:YES];
    //  [[UIApplication sharedApplication] setStatusBarHidden:NO];
}

-(void)setupView{
    _dataArray = [[NSMutableArray alloc]init];
    _aloneArray = [[NSMutableArray alloc]init];
    
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 7.5, mainScreenWidth-20, 30)];
    _textField.backgroundColor = JYColor(242, 242, 242);
    _textField.clearsOnBeginEditing = YES;
    _textField.borderStyle=UITextBorderStyleRoundedRect;
    _textField.placeholder=@"输入群名称/群组号";
    UIImageView *leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 20, 19)];
    leftImageView.image = [UIImage imageNamed:@"Search.png"];
    _textField.leftView = leftImageView;
    _textField.leftViewMode=UITextFieldViewModeAlways;
    [self.view addSubview:_textField];
    
    UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, 44, mainScreenWidth, 3)];
    line.backgroundColor = JYColor(242, 242, 242);
    [self.view addSubview:line];
    
    UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 47, mainScreenWidth, 60)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    
    UISegmentedControl * segmentedCtr = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"最新合唱", @"独唱排名", nil]];
    segmentedCtr.frame = CGRectMake(0, 0, mainScreenWidth, 60);
    segmentedCtr.tintColor = [UIColor whiteColor];
    segmentedCtr.selectedSegmentIndex = 0;
    [segmentedCtr addTarget:self action:@selector(selectedSegmentIndexCount:) forControlEvents:(UIControlEventValueChanged)];
    NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName: [UIColor orangeColor]};
    [segmentedCtr setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName: [UIColor blackColor]};
    [segmentedCtr setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    [bgView addSubview:segmentedCtr];
    
    choruslineView = [[UIView alloc]initWithFrame:CGRectMake(mainScreenWidth/8.0, 50, mainScreenWidth/4.0, 2)];
    choruslineView.backgroundColor = JYColor(222,112, 45);
    [bgView addSubview:choruslineView];
    
    sololineView = [[UIView alloc]initWithFrame:CGRectMake(mainScreenWidth/2.0+mainScreenWidth/8.0, 50, mainScreenWidth/4.0, 2)];
    sololineView.backgroundColor = JYColor(222,112, 45);
    sololineView.hidden = YES;
    [bgView addSubview:sololineView];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 107, mainScreenWidth, 5)];
    lineView.backgroundColor = JYColor(242, 242, 242);
    [self.view addSubview:lineView];
    
    _chorusTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 112, mainScreenWidth, mainScreenHeight-227) style:(UITableViewStylePlain)];
    _chorusTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _chorusTableView.hidden = NO;
    _chorusTableView.delegate = self;
    _chorusTableView.dataSource = self;
    [self.view addSubview:_chorusTableView];
    
    _soloView = [[SoloMusicView alloc]initWithFrame:CGRectMake(0, 112, mainScreenWidth, mainScreenHeight-227)superViewCtr:self];
    _soloView.hidden = YES;
    [self.view addSubview:_soloView];
    
    [self requestData];
    
}

-(void)requestData
{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    NSString *intactURL = [MyBaseUrl stringByAppendingString:@"/api/chorus"];
    
    NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
    
    //    创建请求
    manager.requestSerializer =[AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json"
                     forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                     forHTTPHeaderField:@"Authorization"];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:intactURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSDictionary * bodyDict = [dic objectForKey:@"body"];
        NSArray * contentArray = [bodyDict objectForKey:@"content"];
        for(NSDictionary * contentDict in contentArray){
            KMusicMsgDataModel * msgDataModel = [[KMusicMsgDataModel alloc]init];
            
            msgDataModel.avatar = [contentDict objectForKey:@"avatar"];
            msgDataModel.musicDes = [contentDict objectForKey:@"description"];
            msgDataModel.name = [contentDict objectForKey:@"name"];
            msgDataModel.nickName = [contentDict objectForKey:@"nickName"];
            msgDataModel.chorusNum = [contentDict objectForKey:@"chorusNum"];
            msgDataModel.isSolo = NO;
            msgDataModel.accompanyUrl = contentDict[@"url"];
            msgDataModel.lyric = contentDict[@"lyric"];
            [msgDataModel fixedString];
            [_dataArray addObject:msgDataModel];
        }
        [_chorusTableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

-(void)selectedSegmentIndexCount:(UISegmentedControl *)sender{
    [_textField resignFirstResponder];
    if(sender.selectedSegmentIndex == 0){
        choruslineView.hidden = NO;
        sololineView.hidden = YES;
        _soloView.hidden = YES;
        _chorusTableView.hidden = NO;
    }else if(sender.selectedSegmentIndex ==1){
        choruslineView.hidden = YES;
        sololineView.hidden = NO;
        _soloView.hidden = NO;
        _chorusTableView.hidden = YES;
    }
}

-(void)handleConvert{
    
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MusicChorusTableViewCell"];
    if ( !cell ) {
        cell = [[MusicChorusTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MusicChorusTableViewCell"];
    }
    KMusicMsgDataModel *musicModel = [_dataArray objectAtIndex:indexPath.row];
    [(MusicChorusTableViewCell *)cell updataCellViewWithHeadData:musicModel superViewCtr:self ];
    return cell;
}

#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat contentWidth = tableView.frame.size.width;
    // 用何種字體進行顯示
    UIFont *font = [UIFont systemFontOfSize:13];
    
    // 該行要顯示的內容
    NSString *content = ((KMusicMsgDataModel *)[_dataArray objectAtIndex:indexPath.row]).musicDes;
    // 計算出顯示完內容需要的最小尺寸
    CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeCharacterWrap];
    
    return 100+size.height;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [_textField resignFirstResponder];
    YYplayerVC* yy = [[YYplayerVC alloc]init];
    
    yy.hidesBottomBarWhenPushed = YES;
    
    yy.musicData= [User_information conversion].musicData[0];
    
    [yy setTracks:[Track remoteTracks]];
    
    [self.navigationController pushViewController:yy animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
